package com.linln.admin.system.config;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;

@Configuration
public class MongoConfig {

    @Value("${mongodb.host}")
    private String host;
    @Value("${mongodb.port}")
    private int port;
    @Value("${mongodb.auth}")
    private String auth;

    @Bean
    public MongoClient mongo() throws Exception {
        final ServerAddress address = new ServerAddress(host, port);
        final MongoClientOptions options = MongoClientOptions.builder().build();
        MongoClient mongo = new MongoClient(address, options);
        return mongo;
    }

    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
        MongoTemplate mydatabase = new MongoTemplate(mongo(), "ai-shimao");
        return mydatabase;
    }
}
